package com.liam.chinesevietnamesecorpus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liam.chinesevietnamesecorpus.common.BaseController;
import com.liam.chinesevietnamesecorpus.common.R;
import com.liam.chinesevietnamesecorpus.dto.CorpusLibraryDTO;
import com.liam.chinesevietnamesecorpus.dto.CorpusLibraryUpdateDTO;
import com.liam.chinesevietnamesecorpus.service.CorpusLibraryService;
import com.liam.chinesevietnamesecorpus.vo.CorpusLibraryStatisticsVO;
import com.liam.chinesevietnamesecorpus.vo.CorpusLibraryVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 语料库控制器
 * 
 * @author Liam
 */
@Slf4j
@RestController
@RequestMapping("/corpus")
public class CorpusLibraryController extends BaseController {

    @Autowired
    private CorpusLibraryService corpusLibraryService;

    /**
     * 创建语料库
     */
    @PostMapping("/libraries/create")
    public R<CorpusLibraryVO> createCorpusLibrary(@Valid @RequestBody CorpusLibraryDTO corpusLibraryDTO) {
        log.info("创建语料库请求：{}", corpusLibraryDTO.getName());
        CorpusLibraryVO corpusLibraryVO = corpusLibraryService.createCorpusLibrary(corpusLibraryDTO);
        log.info("语料库创建成功：{}", corpusLibraryDTO.getName());
        return R.ok("创建成功", corpusLibraryVO);
    }

    /**
     * 获取语料库信息
     */
    @GetMapping("/libraries/get/{id}")
    public R<CorpusLibraryVO> getCorpusLibrary(@PathVariable Long id) {
        CorpusLibraryVO corpusLibraryVO = corpusLibraryService.getCorpusLibraryById(id);
        return R.ok(corpusLibraryVO);
    }

    /**
     * 更新语料库信息
     */
    @PutMapping("/libraries/update/{id}")
    public R<CorpusLibraryVO> updateCorpusLibrary(@PathVariable Long id, @Valid @RequestBody CorpusLibraryUpdateDTO corpusLibraryUpdateDTO) {
        log.info("更新语料库请求：语料库ID={}", id);
        CorpusLibraryVO corpusLibraryVO = corpusLibraryService.updateCorpusLibrary(id, corpusLibraryUpdateDTO);
        log.info("语料库信息更新成功：语料库ID={}", id);
        return R.ok("更新成功", corpusLibraryVO);
    }

    /**
     * 删除语料库
     */
    @DeleteMapping("/libraries/delete/{id}")
    public R<Void> deleteCorpusLibrary(@PathVariable Long id) {
        log.info("删除语料库请求：语料库ID={}", id);
        corpusLibraryService.deleteCorpusLibrary(id);
        log.info("语料库删除成功：语料库ID={}", id);
        return R.ok("删除成功");
    }

    /**
     * 获取语料库列表（分页）
     */
    @GetMapping("/libraries/list")
    public R<IPage<CorpusLibraryVO>> getCorpusLibraryList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String domain) {
        
        log.info("获取语料库列表请求：page={}, size={}, keyword={}, domain={}", page, size, keyword, domain);
        IPage<CorpusLibraryVO> libraryPage = corpusLibraryService.getCorpusLibraryList(page, size, keyword, domain);
        return R.ok(libraryPage);
    }

    /**
     * 获取语料库统计信息
     */
    @GetMapping("/libraries/statistics/{id}")
    public R<CorpusLibraryStatisticsVO> getCorpusLibraryStatistics(@PathVariable Long id) {
        log.info("获取语料库统计信息请求：语料库ID={}", id);
        CorpusLibraryStatisticsVO statistics = corpusLibraryService.getCorpusLibraryStatistics(id);
        return R.ok(statistics);
    }
}
